﻿Imports System.Data.OleDb


Public Class AddJDMovie
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Submit.Click

        Dim newFilename As String
        newFilename = Guid.NewGuid.ToString() & getSuffix(JDF_Picture.FileName)
        If JDF_Picture.HasFile Then
            JDF_Picture.SaveAs(Server.MapPath("Pictures") & "/" & newFilename)
        End If

        Dim SqlString As String = "INSERT INTO JDF_List(Movie_Name,Rating,Director_Name,Role_of_JD,Description,Poster,Movie_Category,Movie_Business,AddedBy) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9)"

        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oledbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", tb_MovieName.Text)
        cmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f3", tb_DirectorName.Text)
        cmd.Parameters.AddWithValue("@f4", tb_RoleOfJD.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f6", "Pictures/" & newfilename)
        cmd.Parameters.AddWithValue("@f7", tb_MovieCategory.Text)
        cmd.Parameters.AddWithValue("@f8", tb_BusinessDone.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)

        oledbConn.Open()
        If tb_MovieName.Text = Nothing Or tb_DirectorName.Text = Nothing Or tb_RoleOfJD.Text = Nothing Or tb_Description.Text = Nothing Or
            newFilename = Nothing Or tb_MovieCategory.Text = Nothing Or tb_BusinessDone.Text = Nothing Then
            Me.Btn_Submit.Enabled = False
        Else
            Me.Btn_Submit.Enabled = True
        End If


        cmd.ExecuteNonQuery()

        oledbConn.Close()
        cmd.Dispose()
        oledbConn.Dispose()
        Response.Redirect("JDMovieList.aspx")


    End Sub
    
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function


End Class